using System;
using System.Collections.Generic;
using System.Text;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;
using Microsoft.WindowsMobile.PocketOutlook;

namespace PocketGCal
{
    /// <summary>
    /// Translates between google and outlook event types
    /// </summary>
    class GoBetween
    {
        /// <summary>
        /// Set properties on outlook appoinment from google calendar entry
        /// </summary>
        /// <param name="google">Google calendar entry</param>
        /// <param name="outlook">PocketOutlook appointment</param>
        public static void GoogleToOutlook(EventEntry google, Appointment outlook, GFeed feed)
        {
            outlook.Subject = google.Title.Text;
            outlook.Start = DateTime.SpecifyKind(google.Times[0].StartTime, DateTimeKind.Utc).ToLocalTime();
            outlook.End = DateTime.SpecifyKind(google.Times[0].EndTime,DateTimeKind.Utc).ToLocalTime();
            outlook.Body = google.Content.Content.ToString();
            outlook.Location = google.Locations.Count > 0 ? google.Locations[0].ValueString : string.Empty;

            if (!outlook.Properties.Contains("GCalID") || outlook.Properties["GCalID"]==null)
            {
                outlook.Properties.Add("GCalID", "".GetType(), PimPropertyFlags.DoNotReplicate);
                outlook.Properties["GCalID"] = google.Id.AbsoluteUri;
            }
            outlook.Update();
        }

        /// <summary>
        /// Sets properties on google entry from outlook appointment
        /// </summary>
        /// <param name="outlook">PocketOutlook appointment</param>
        /// <param name="google">Google calendar entry</param>
        public static void OutlookToGoogle(Appointment outlook, EventEntry google, GFeed feed)
        {

            google.Title = new AtomTextConstruct(AtomTextConstructElementType.Title, outlook.Subject);
            google.Content.Content = outlook.Body;
            google.Authors.Add(new AtomPerson(AtomPersonType.Unknown,"PocketGCal"));
            if (google.Locations.Count == 0)
            {
                google.Locations.Add(new Where("", "", outlook.Location));
            }
            else
            {
                google.Locations[0] = new Where("", "", outlook.Location);
            }
            When gTimes = new When();
            gTimes.StartTime = DateTime.SpecifyKind(outlook.Start,DateTimeKind.Local).ToUniversalTime();
            gTimes.EndTime = DateTime.SpecifyKind(outlook.End, DateTimeKind.Local).ToUniversalTime();
            google.Times.Add(gTimes);

        }

        /// <summary>
        /// Creates new Outlook Appointment and sets values from google calendar entry 
        /// </summary>
        /// <param name="google">Google calendar entry</param>
        /// <param name="outlook">PocketOutlook session</param>
        public static void AddGoogleToOutlook(EventEntry google, OutlookSession outlook, GFeed feed)
        {
            Appointment newApp = outlook.Appointments.Items.AddNew();
            GoogleToOutlook(google, newApp, feed);
            newApp.Update();
        }

        /// <summary>
        /// Creates new Google Calendar entry and sets values from Outlook
        /// </summary>
        /// <param name="outlook">PocketOutlook Appointment</param>
        /// <param name="google">Google Calendar Service</param>
        public static void AddOutlookToGoogle(Appointment outlook, GFeed feed)
        {
            EventEntry newEntry = new EventEntry();
            OutlookToGoogle(outlook, newEntry, feed);
            EventEntry returned = GoogleCalendar.Service.Insert(GoogleCalendar.Uri, newEntry) as EventEntry;

            if (!outlook.Properties.Contains("GCalID") || outlook.Properties["GCalID"] == null)
            {
                outlook.Properties.Add("GCalID", "".GetType(), PimPropertyFlags.DoNotReplicate);
                outlook.Properties["GCalID"] = returned.Id.AbsoluteUri;
            }
            outlook.Update();
        }

    }
}
